package com.rs.controller.Rs;


import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.rs.controller.Rs.BumenJFrame.SelBumen;
import com.rs.controller.Rs.ZhiwuJFrame.SelZhiwu;
import com.rs.model.Department;
import com.rs.model.Position;
import com.rs.service.rs.DepartmentService;
import com.rs.util.SpringContextUtils;


public class ZhiwuJFrame extends JFrame {

	private static final long serialVersionUID = 1L;

	 public class AddZhiwu extends ZhiwuJFrame {
		 
		private JPanel contentPane;

		private static final long serialVersionUID = 1L;

		private JTextField zwname;//职务名称
		
		private JComboBox<String> bmname;//部门名称
		
		private JComboBox<Integer> porank;//职务级别
		
		private DepartmentService dts = SpringContextUtils.getBean(DepartmentService.class);
		
		public AddZhiwu(List<Department> ll) {
			super();
			//设置标题
			setTitle("添加职务");
			//设置大小
			setBounds(250, 50, 300, 200);
			//主体
			contentPane = new JPanel();
			setContentPane(contentPane);
			contentPane.setLayout(null);

			//创建1个字体实bai例
			Font font = new Font("宋体", Font.PLAIN, 15);

			//部门名称	
			JLabel names = new JLabel("新职务名称：");
			JLabel  a  = new JLabel("所属部门 ：");
			JLabel  b  = new JLabel("所属级别 ：");

			names.setFont(font);
			names.setBounds(50, 25, 120, 20);
			a.setFont(font);
			a.setBounds(50, 50, 120, 20);
			b.setFont(font);
			b.setBounds(50, 75, 120, 20);
			zwname = new JTextField();
			zwname.setBounds(140, 25, 100, 20);
			zwname.setColumns(10);
			bmname = new JComboBox<String>();
			for(Department dd:ll) {
				bmname.addItem(null);
				bmname.addItem(dd.getDe_name());
			}
			bmname.setBounds(140, 50, 100, 22);
			bmname.setFont(font);
			porank = new JComboBox<Integer>();
			porank.addItem(null);
			porank.addItem(1);
			porank.addItem(2);
			porank.addItem(3);
			porank.setBounds(140, 75, 100, 20);
			porank.setFont(font);
			JButton tijiao = new JButton("提交");
			tijiao.setFont(font);
            tijiao.setBounds(170, 100, 70, 20);
            contentPane.add(names);
            contentPane.add(a);
            contentPane.add(b);
			contentPane.add(zwname);
            contentPane.add(bmname);
            contentPane.add(porank);
            contentPane.add(tijiao);
            tijiao.addActionListener(new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				Position pp = new Position();
    				String po_name = zwname.getText();
    				String bmname1 = (String) bmname.getSelectedItem();
    				int po_rank =  (int) porank.getSelectedItem();
    				pp.setPo_name(po_name);
    				pp.setBmname(bmname1);
    				pp.setPo_rank(po_rank);
    				dts.addZhiwu(pp);
    			    showMessageDialog("添加成功");
    			}
    		});
		}	
	}
	 
	 public class SelZhiwu extends ZhiwuJFrame {
		   
		    private JPanel contentPane;
			
		    private DepartmentService dts = SpringContextUtils.getBean(DepartmentService.class);
			
			private static final long serialVersionUID = 1L;
		
			public SelZhiwu(List<Position> ll) {
			    super();
				//设置标题
				setTitle("职务列表");
				//设置大小
				setBounds(250,50,620,480);
				//主体
				contentPane = new JPanel();
				contentPane.setBorder(new EmptyBorder(20,20,620,480));
				setContentPane(contentPane);
				contentPane.setLayout(null);
				//创建1个字体实bai例
				Font font = new Font("宋体", Font.PLAIN, 15);
				
				JLabel label = new JLabel("编号");
				label.setFont(font);
				label.setBounds(50,25,70,20);
				contentPane.add(label);
				
				JLabel label1 = new JLabel("职务");
				label1.setFont(font);
				label1.setBounds(155,25,100,20);
				contentPane.add(label1);
				
				JLabel label2 = new JLabel("部门");
				label2.setFont(font);
				label2.setBounds(255,25,100,20);
				contentPane.add(label2);
				
				JLabel label3 = new JLabel("级别");
				label3.setFont(font);
				label3.setBounds(350,25,100,20);
				contentPane.add(label3);
				
				JLabel label4 = new JLabel("操作");
				label4.setFont(font);
				label4.setBounds(480,25,70,20);
				contentPane.add(label4);
				
				for(int i=0;i<ll.size();i++) {
					final Position l1 = ll.get(i);
					JLabel label5 = new JLabel(l1.getPo_id()+"");
					label5.setFont(font);
					label5.setBounds(50,25*(i+2),70,20);
					contentPane.add(label5);
					
					JLabel label6 = new JLabel(l1.getPo_name());
					label6.setFont(font);
					label6.setBounds(135,25*(i+2),100,20);
					contentPane.add(label6);
					
					JLabel label7 = new JLabel(l1.getBmname());
					label7.setFont(font);
					label7.setBounds(245,25*(i+2),100,20);
					contentPane.add(label7);
					
					JLabel label8 = new JLabel(l1.getPo_rank()+"");
					label8.setFont(font);
					label8.setBounds(360,25*(i+2),100,20);
					contentPane.add(label8);
					
					JButton del = new JButton("删除");
					del.setFont(font);
					del.setBounds(425,25*(i+2),70,20);
					contentPane.add(del);
					del.addActionListener(new ActionListener() {
		    			@Override
		    			public void actionPerformed(ActionEvent e) {
		    				Object[] options = {"是(Y)","否(N)"};
		    	            int a = JOptionPane.showOptionDialog(null,"您确定要删除这个职务吗？","提醒",
		    	            JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,options,options[0]);
		    	            if(a==1) {
		    	            	return;
		    	            }else if(a==0){
		    	            	String result = dts.delZhiwu(l1.getPo_id());
		    	            	List<Position> l2 = dts.positionSelectAll();
		        				if(result.equals("删除成功")){
		        					showMessageDialog("删除成功");
		        					dispose();
		        					new ZhiwuJFrame().new SelZhiwu(l2).setVisible(true);
		        				}else {
		        					showMessageDialog("删除失败");
		        				}
		    	            }
		    				
		    			}
		    		});
					
					JButton update = new JButton("编辑");
					update.setFont(font);
					update.setBounds(495,25*(i+2),70,20);
					contentPane.add(update);
					update.addActionListener(new ActionListener() {
		    			@Override
		    			public void actionPerformed(ActionEvent e) {
		    				List<Department> ll = dts.selBuMenAll();
		    				new ZhiwuJFrame().new UpDateZhiwu(l1,ll).setVisible(true);
		    			}
		    		});

				}	
			}			
		}
	   public class UpDateZhiwu extends ZhiwuJFrame {
			 
			private JPanel contentPane;

			private static final long serialVersionUID = 1L;

			private JTextField zwname;//职务名称
			
			private JComboBox<String> bmname;//部门名称
			
			private JComboBox<Integer> porank;//职务级别
			
			private DepartmentService dts = SpringContextUtils.getBean(DepartmentService.class);
			
			public UpDateZhiwu(final Position l1,List<Department> ll) {
				super();
				//设置标题
				setTitle("编辑职务信息");
				//设置大小
				setBounds(250, 50, 300, 200);
				//主体
				contentPane = new JPanel();
				setContentPane(contentPane);
				contentPane.setLayout(null);

				//创建1个字体实bai例
				Font font = new Font("宋体", Font.PLAIN, 15);

				//部门名称	
				JLabel names = new JLabel("职务名称：");
				JLabel  a  = new JLabel("所属部门 ：");
				JLabel  b  = new JLabel("所属级别 ：");

				names.setFont(font);
				names.setBounds(50, 25, 120, 20);
				a.setFont(font);
				a.setBounds(50, 50, 120, 20);
				b.setFont(font);
				b.setBounds(50, 75, 120, 20);
				zwname = new JTextField(l1.getPo_name());
				zwname.setBounds(140, 25, 100, 20);
				zwname.setColumns(10);
				bmname = new JComboBox<String>();
				bmname.addItem(l1.getBmname());
				for(Department dd:ll) {
					if(dd.getDe_name().equals(l1.getBmname())){
						continue;
					};
					bmname.addItem(dd.getDe_name());
				}
				bmname.setBounds(140, 50, 100, 22);
				bmname.setFont(font);
				porank = new JComboBox<Integer>();
				porank.addItem(l1.getPo_rank());
				for(int i=1;i<=3;i++){
					if(i==l1.getPo_rank()) {
						continue;
					}
					porank.addItem(i);
				}
				porank.setBounds(140, 75, 100, 20);
				porank.setFont(font);
				JButton tijiao = new JButton("提交");
				tijiao.setFont(font);
	            tijiao.setBounds(170, 100, 70, 20);
	            contentPane.add(names);
	            contentPane.add(a);
	            contentPane.add(b);
				contentPane.add(zwname);
	            contentPane.add(bmname);
	            contentPane.add(porank);
	            contentPane.add(tijiao);
	            tijiao.addActionListener(new ActionListener() {
	    			@Override
	    			public void actionPerformed(ActionEvent e) {
	    				String po_name = zwname.getText();
	    				String bmname1 = (String) bmname.getSelectedItem();
	    				int po_rank =  (int) porank.getSelectedItem();
	    				l1.setPo_name(po_name);
	    				l1.setBmname(bmname1);
	    				l1.setPo_rank(po_rank);
	    				String result = dts.upDateZhiWu(l1);
	    				List<Position> l2 = dts.positionSelectAll();
	    				if(result.equals("编辑成功")){
	    					showMessageDialog("编辑成功");
	    					dispose();
	    					new ZhiwuJFrame().new SelZhiwu(l2).setVisible(true);
	    				}else {
	    					showMessageDialog("编辑失败");
	    				}
	    			}
	    		});
	    	}
		}
	 
	//提示弹窗
		 public void showMessageDialog(String massage){
		  JOptionPane.showMessageDialog(this,massage);
		 }
}
